package com.ming.controller;

import com.ming.dao.CustomerMapper;
import com.ming.dao.RecipientMapper;
import com.ming.dao.StaffMapper;
import com.ming.dao.WaybillMapper;
import com.ming.pojo.Waybill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/15 10:07
 */
@Controller
public class WaybillController {
    @Autowired
    WaybillMapper waybillMapper;

    @Autowired
    StaffMapper staffMapper;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    RecipientMapper recipientMapper;

    @RequestMapping("/waybills")
    public String list(Model model){
        List<Waybill> waybills = waybillMapper.queryWaybills();
        model.addAttribute("waybills",waybills);
        return "waybills/list";
    }

    @RequestMapping("/staffWaybills/{username}")
    public String staffPList(@PathVariable("username") String username,Model model){
        Integer sid = staffMapper.queryStaffIdByName(username);
        List<Waybill> waybills = waybillMapper.queryWaybillBySid(sid);
        model.addAttribute("waybills",waybills);
        return "waybills/staffWList";
    }
    @RequestMapping("/userOWaybills/{username}")
    public String userOPList(@PathVariable("username") String username,Model model){
        Integer cid = customerMapper.queryCustomerByName(username);
        if(cid != null){
            List<Waybill> waybills = waybillMapper.queryWaybillByCid(cid);
            model.addAttribute("waybills",waybills);
        }
        return "waybills/userWList";
    }
    @RequestMapping("/userWaybills/{username}")
    public String userPList(@PathVariable("username") String username,Model model){
        Integer rid = recipientMapper.queryRecipientByName(username);
        if (rid != null){
            List<Waybill> waybills = waybillMapper.queryWaybillByRid(rid);
            model.addAttribute("waybills",waybills);
        }
        return "waybills/userWList";
    }

    @GetMapping("/waybill")
    public String toAddPage(Model model){
        //查出所有部门信息
        List<Waybill> waybills = waybillMapper.queryWaybills();
        model.addAttribute("waybills",waybills);
        return "waybills/add";
    }

    @PostMapping("/waybill")
    public String addWaybill(Waybill waybill){
        waybill.setWid(waybillMapper.getMId());
        waybillMapper.addWaybill(waybill);
        return "redirect:/waybills";
    }

    //去客户的修改页面
    @GetMapping("/waybill/{id}")
    public String toUpdateWaybill(@PathVariable("id") int id, Model model) {
        //查出原来的数据
        Waybill waybill = waybillMapper.queryWaybillById(id);
        model.addAttribute("waybill", waybill);
        return "waybills/update";
    }

    @PostMapping("/updateWaybill")
    public String updateWaybill(Waybill waybill) {
        waybillMapper.updateWaybill(waybill);
        return "redirect:/waybills";
    }


    @GetMapping("/deleteWaybill/{id}")
    public String deleteWaybill(@PathVariable("id")Integer id){
        waybillMapper.deleteWaybill(id);
        return "redirect:/waybills";
    }

}
